package com.cy.mall.web.front.enter;

import com.cy.mall.model.PageBean;
import com.cy.mall.model.Result;
import com.cy.mall.model.ao.enter.*;
import com.cy.mall.model.domain.enter.Enter;
import com.cy.mall.model.dto.shuidai.EnterEvaluateRequestParamDTO;
import com.cy.mall.model.query.enter.EnterListQuery;
import com.cy.mall.model.vo.enter.*;
import com.cy.mall.service.enter.EnterService;
import com.cy.mall.service.shuidai.ShuiDaiService;
import com.cy.mall.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Description 前台-我的企业
 * @Author tanyilin
 * @Date 2019/3/20 14:09
 */
@Slf4j
@RestController
@RequestMapping("/customer/enter")
@Api(tags = "前台-我的企业")
public class EnterController extends BaseController {

    @Autowired
    private EnterService enterService;
    @Autowired
    private ShuiDaiService shuiDaiService;

    @ApiOperation("添加企业")
    @PostMapping("/do/add")
    public Result doAddEnter(@Validated EnterAddAO addAO, BindingResult bindingResult) {

        this.checkError(bindingResult);

        Enter enter = new Enter();
        BeanUtils.copyProperties(addAO, enter);
        enter.setCustomerId(this.getLoginCustomer().getCustomerId());
        enterService.addEnter(enter);

        return ok();
    }

    @ApiOperation("编辑修改企业")
    @PostMapping("/do/edit")
    public Result doEditEnter(@Validated EnterEditAO editAO, BindingResult bindingResult) {
        this.checkError(bindingResult);
        Enter enter = new Enter();
        BeanUtils.copyProperties(editAO, enter);
        enterService.updateEnter(enter);
        return ok();
    }

    @ApiOperation("企业详情")
    @GetMapping("/detail")
    public Result<Enter> queryEnterDetail(@RequestParam("enterId") Long enterId) {
        Enter enter = enterService.queryEnterDetail(enterId);
        return ok("", enter);
    }

    @ApiOperation("我的企业列表")
    @GetMapping("/list")
    public PageBean<EnterListVO> queryEnterList(EnterListQuery query) {
        query.setCustomerId(this.getLoginCustomer().getCustomerId());
        PageBean<EnterListVO> pageBean = enterService.queryEnterList(query);
        return pageBean;
    }

    @ApiOperation("查询评估信息是否填写完成")
    @GetMapping("/query/evaluate/status")
    public Result<EnterEvaluateStatusVO> queryEvaluateStatus(@RequestParam("enterId") Long enterId) {
        EnterEvaluateStatusVO result = enterService.queryEvaluateStatus(enterId);
        return ok("", result);
    }

    @ApiOperation("获取企业基本信息")
    @GetMapping("/get/enter/base/info")
    public Result<EnterBaseInfoDetailVO> getEnterBaseInfo(@RequestParam("enterId") Long enterId) {

        EnterBaseInfoDetailVO result = enterService.getEnterBaseInfo(enterId);
        return ok("", result);
    }

    @ApiOperation("保存企业基本信息")
    @PostMapping("/do/save/enter/base/info")
    public Result doSaveEnterBaseInfo(@Validated EnterBaseInfoSaveAO saveAO, BindingResult bindingResult) {
        this.checkError(bindingResult);
        enterService.doSaveEnterBaseInfo(saveAO);
        return ok();
    }

    @ApiOperation("获取企业股东信息")
    @GetMapping("/get/shareholder/info")
    public Result<List<EnterShareholderInfoVO>> getEnterShareholderInfo(@RequestParam("enterId") Long enterId) {
        List<EnterShareholderInfoVO> list = enterService.getEnterShareholderInfo(enterId);
        return ok("获取成功", list);
    }

    @ApiOperation("保存企业股东信息")
    @PostMapping("/do/save/shareholder/info")
    public Result doSaveShareholderInfo(@RequestBody @Validated EnterShareholderInfoSaveAO saveAO, BindingResult bindingResult) {

        this.checkError(bindingResult);
        enterService.doSaveShareholderInfo(saveAO);
        return ok();
    }

    @ApiOperation("获取企业管理人员信息")
    @GetMapping("/get/manager/info")
    public Result<List<EnterManagerInfoVO>> getEnterManagerInfoVO(@RequestParam("enterId") Long enterId) {
        List<EnterManagerInfoVO> list = enterService.getEnterManagerInfo(enterId);
        return ok("获取成功", list);
    }

    @ApiOperation("保存企业管理人员信息")
    @PostMapping("/do/save/manager/info")
    public Result doSaveManagerInfo(@RequestBody @Validated EnterManagerInfoSaveAO saveAO, BindingResult bindingResult) {

        this.checkError(bindingResult);
        enterService.doSaveEnterManagerInfo(saveAO);
        return ok();
    }

    @ApiOperation("获取企业对外投资信息")
    @GetMapping("/get/investment/info")
    public Result<List<EnterInvestmentInfoVO>> getEnterInvestmentInfoVO(@RequestParam("enterId") Long enterId) {
        List<EnterInvestmentInfoVO> list = enterService.getEnterInvestmentInfo(enterId);
        return ok("获取成功", list);
    }

    @ApiOperation("保存企业对外投资信息")
    @PostMapping("/do/save/investment/info")
    public Result doSaveInvestmentInfo(@RequestBody @Validated EnterInvestmentInfoSaveAO saveAO, BindingResult bindingResult) {

        this.checkError(bindingResult);
        enterService.doSaveEnterInvestmentInfo(saveAO);
        return ok();
    }

    @ApiOperation("获取企业申报数据明细")
    @GetMapping("/get/declareData/detail")
    public Result<List<EnterDeclareDataDetailVO>> getEnterDeclareDataDetail(@RequestParam("enterId") Long enterId) {
        List<EnterDeclareDataDetailVO> list = enterService.getEnterDeclareDataDetail(enterId);
        return ok("获取成功", list);
    }

    @ApiOperation("保存企业申报数据明细")
    @PostMapping("/do/save/declareData/detail")
    public Result doSaveEnterDeclareDataDetail(@RequestBody @Validated EnterDeclareDataDetailSaveAO saveAO, BindingResult bindingResult) {

        this.checkError(bindingResult);
        enterService.doSaveEnterDeclareDataDetail(saveAO);
        return ok();
    }

    @ApiOperation("获取企业利润报表信息")
    @GetMapping("/get/enter/corporate/profit")
    public Result<Map<String, Object>> getEnterCorporateProfit(@RequestParam("enterId") Long enterId) {
        Map<String, Object> map = enterService.getEnterCorporateProfit(enterId);
        return ok("", map);
    }

    @ApiOperation("保存企业利润信息报表")
    @PostMapping("/do/save/enter/corporate/profit")
    public Result doSaveEnterCorporateProfit(@RequestBody @Validated EnterCorporateProfitSaveAO saveAO, BindingResult bindingResult) {
        this.checkError(bindingResult);
        enterService.doSaveEnterCorporateProfit(saveAO);
        return ok();
    }

    @ApiOperation("获取企业财务报表信息")
    @GetMapping("/get/enter/finance/info")
    public Result<Map<String, Object>> getEnterFinanceInfo(@RequestParam("enterId") Long enterId) {
        Map<String, Object> map = enterService.getEnterFinanceInfo(enterId);
        return ok("", map);
    }

    @ApiOperation("保存企业财务信息报表")
    @PostMapping("/do/save/enter/finance/info")
    public Result doSaveEnterFinanceInfo(@RequestBody @Validated EnterFinanceInfoSaveAO saveAO, BindingResult bindingResult) {
        this.checkError(bindingResult);
        enterService.doSaveEnterFinanceInfo(saveAO);
        return ok();
    }

    @ApiOperation("进行企业评估")
    @GetMapping("/do/evaluate")
    public Result doEvaluate(@RequestParam("enterId") Long enterId) {

        //查询并封装请求参数
        Map<String, Object> requestParamMap = enterService.getEnterEvaluateRequestParam(enterId);
        //请求api进行评估
        Result<Map<String, Object>> result = shuiDaiService.postToEvaluate(requestParamMap);
        if(!result.isOk()) {
            return result;
        }
        Map<String, Object> resultMap = result.getData();
        resultMap.put("enterId", enterId);
        enterService.doSaveEvaluateRecord(resultMap);
        return ok();
    }

    @ApiOperation("企业评估接口联调")
    @PostMapping("/test/evaluate/api")
    public Result testEvaluateApi(@RequestBody Map<String, Object> requestParamMap) {
        //请求api进行评估
        Result<Map<String, Object>> result = shuiDaiService.postToEvaluate(requestParamMap);
        return result;
    }

}
